const path = require("path");
const eslintWebpackPlugin = require("eslint-webpack-plugin");
const htmlWebpackPlugin = require("html-webpack-plugin");
const postCssPxToViewport = require("postcss-px-to-viewport");

const postcssLoader = {
  loader: "postcss-loader",
  options: {
    postcssOptions: {
      plugins: [
        "postcss-preset-env",
        postCssPxToViewport({
          unitToConvert: "px",
          viewportWidth: 375,
          unitPrecision: 5,
          propList: ["*"],
          viewportUnit: "vw",
          fontViewportUnit: "vw",
          selectorBlackList: [],
          minPixelValue: 1,
          mediaQuery: false,
          replace: true,
          exclude: undefined,
          include: undefined,
          landscape: false,
          landscapeUnit: "vw",
          landscapeWidth: 568,
        }),
      ],
    },
  },
};

module.exports = {
  // 入口
  entry: "./core/src/index.js",
  // 输出
  output: {
    path: undefined,
    filename: "scripts/main.js",
    publicPath: "/",
  },
  // 加载器
  module: {
    rules: [
      {
        test: /\.css$/i, // 只检测以.css结尾的文件
        use: [
          // 执行顺序：从右到左，或从下到上
          "style-loader", // style-loader: 将js中的css通过创建style标签添加到html文件中
          "css-loader", // css-loader: 将css资源编译成commonjs的模块到js中
          postcssLoader,
        ],
      },
      {
        test: /\.less$/i,
        use: [
          "style-loader",
          "css-loader",
          postcssLoader,
          "less-loader", // 将less文件编译为css文件
        ],
      },
      {
        test: /\.(png|jpe?g|gif|webp)$/i,
        type: "asset",
        parser: {
          dataUrlCondition: {
            maxSize: 10 * 1024, // 10kb
          },
        },
        generator: {
          filename: "static/images/[hash:10][ext][query]",
        },
      },
      {
        test: /\.(ttf|woff2?)$/i,
        type: "asset/resource",
        generator: {
          filename: "static/iconfonts/[hash:10][ext][query]",
        },
      },
      {
        test: /\.(mp3|mp4)$/i,
        type: "asset/resource",
        generator: {
          filename: "static/medias/[hash:10][ext][query]",
        },
      },
      {
        test: /\.html$/i,
        loader: "html-loader",
        options: {
          sources: true,
        },
      },
      {
        test: /\.(?:js|mjs|cjs)$/,
        exclude: /node_modules/,
        use: {
          loader: "babel-loader",
          options: {
            presets: [["@babel/preset-env", { targets: "defaults" }]],
          },
        },
      },
    ],
  },
  plugins: [
    new htmlWebpackPlugin({
      template: path.resolve(__dirname, "../public/index.html"),
      filename: "index.html",
    }),
    new eslintWebpackPlugin({
      context: path.resolve(__dirname, "./core/src/"),
      configType: "flat",
      cache: true,
      cacheLocation: "./.eslintcache",
    }),
  ],
  mode: "development",
  devServer: {
    /* static: {
      directory: path.resolve(__dirname, "./public"),
      publicPath: "/",
    }, */
    host: "localhost",
    port: "8888",
    open: true,
    hot: true,
    watchFiles: ["./public/index.html"],
    // compress: true,
  },
};
